IF EXISTS ( SELECT  *
            FROM    dbo.sysobjects
            WHERE   id = OBJECT_ID(N'[CGO_CharityGetList]')
                    AND OBJECTPROPERTY(id, N'IsProcedure') = 1 ) 
  DROP PROCEDURE CGO_CharityGetList
GO
  

SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO


/** Create Stored Procedures **/


CREATE PROCEDURE dbo.CGO_CharityGetList 
  @PortalID INT,
  @EventID INT
AS 
  IF @EventID IS NULL
  BEGIN
  	SET @EventID = 0
  END
  
  SELECT  CharityID ,
          PortalID ,          
          Name ,
          Description ,
          Contact ,
          WebSite ,
          M.Email ,
          Telephone ,
          Fax ,
          Address ,
          Category ,
          ImageURL,
          ImageType,
          ImageHeight,
          ImageWidth,          
          Status,
          ImageDisplay,
          CreatedByID,
          UC.Username AS CreateByUserName,
          CreatedDate,
          LastUpdatedID,
          UU.Username AS LastUpdateUserName,
          LastUpdateDate
  FROM    dbo.CGO_Charity M
  LEFT JOIN dbo.Users UC ON M.CreatedByID = UC.UserID
  LEFT JOIN dbo.Users UU ON M.LastUpdatedID = UU.UserID
  WHERE   PortalID = @PortalID
          AND ( @EventID = 0
                OR CharityID IN ( SELECT  CharityID
                                  FROM    dbo.CGO_EventCharity
                                  WHERE   EventID = @EventID
                                          AND PortalID = @PortalID )
              )
GO
